package com.ruoyi.crm.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.crm.domain.CustomerProduct;
import com.ruoyi.crm.mapper.OrderPurchaseMapper;
import com.ruoyi.crm.domain.OrderPurchase;
import com.ruoyi.crm.service.IOrderPurchaseService;

/**
 * 采购订单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-31
 */
@Service
public class OrderPurchaseServiceImpl implements IOrderPurchaseService
{
    @Autowired
    private OrderPurchaseMapper orderPurchaseMapper;

    /**
     * 查询采购订单
     *
     * @param id 采购订单主键
     * @return 采购订单
     */
    @Override
    public OrderPurchase selectOrderPurchaseById(String id)
    {
        return orderPurchaseMapper.selectOrderPurchaseById(id);
    }

    /**
     * 查询采购订单列表
     *
     * @param orderPurchase 采购订单
     * @return 采购订单
     */
    @Override
    public List<OrderPurchase> selectOrderPurchaseList(OrderPurchase orderPurchase)
    {
        return orderPurchaseMapper.selectOrderPurchaseList(orderPurchase);
    }

    /**
     * 新增采购订单
     *
     * @param orderPurchase 采购订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOrderPurchase(OrderPurchase orderPurchase)
    {
        int rows = orderPurchaseMapper.insertOrderPurchase(orderPurchase);
        insertCustomerProduct(orderPurchase);
        return rows;
    }

    /**
     * 修改采购订单
     *
     * @param orderPurchase 采购订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOrderPurchase(OrderPurchase orderPurchase)
    {
        orderPurchase.setUpdateTime(DateUtils.getNowDate());
        orderPurchaseMapper.deleteCustomerProductByCustomerName(orderPurchase.getId());
        insertCustomerProduct(orderPurchase);
        return orderPurchaseMapper.updateOrderPurchase(orderPurchase);
    }

    /**
     * 批量删除采购订单
     *
     * @param ids 需要删除的采购订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrderPurchaseByIds(String[] ids)
    {
        return orderPurchaseMapper.deleteOrderPurchaseByIds(ids);
    }

    /**
     * 删除采购订单信息
     *
     * @param id 采购订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderPurchaseById(String id)
    {
        orderPurchaseMapper.deleteCustomerProductByCustomerName(id);
        return orderPurchaseMapper.deleteOrderPurchaseById(id);
    }

    /**
     * 新增顾客产品信息信息
     *
     * @param orderPurchase 采购订单对象
     */
    public void insertCustomerProduct(OrderPurchase orderPurchase)
    {
        List<CustomerProduct> customerProductList = orderPurchase.getCustomerProductList();
        String id = orderPurchase.getId();
        if (StringUtils.isNotNull(customerProductList))
        {
            List<CustomerProduct> list = new ArrayList<CustomerProduct>();
            for (CustomerProduct customerProduct : customerProductList)
            {
                customerProduct.setCustomerName(id);
                list.add(customerProduct);
            }
            if (list.size() > 0)
            {
                orderPurchaseMapper.batchCustomerProduct(list);
            }
        }
    }
}